const xlsx = require('node-xlsx')
const fs = require('fs')
const fse = require('fs-extra')
const config = require('../config')
const path = require('path')

function exportXlsx(data) {
  // 文件上传目录
  const filePath = '/temp/'
  const fileDir = path.join(config.storageDir, filePath)
  const name = Date.now() + '.csv'

  const filename = path.join(fileDir, name)

  // 确保目录存在
  fse.ensureDirSync(fileDir)

  var buffer = xlsx.build([{ name: 'sheetName', data: data }])

  fs.writeFileSync(filename, buffer, 'binary')

  return {
    url: config.host + filePath + name,
    filename,
  }
}

module.exports = exportXlsx
